<?php
if(!defined('MIRAGE_MODE')) die('Forbidden');
/**
 * Description of Checker
 * Date Created Sep 3, 2011 8:52:50 PM
 * @author Bryan Salazar
 */
class Checker {
   
   private static $_config_file;
   private static $_config;
   
   private static function _checkDirExist($dir) {
      if(!file_exists($dir) || !is_dir($dir))
      throw new SystemException('Directory ' . $dir . ' not exist');
   }
   
   private static function _checkKeyInConfig($key) {
      if(!isset(self::$_config[$key]))
         throw new SystemException('Key "' . $key . '" not exist in ' . 
            self::$_config_file); 
   }
   
   public static function checkConfigAndDirectory($config,$config_file) {
      $app_path = Mirage::app()->getAppPath();
      self::$_config_file = $config_file;
      self::$_config = $config;
      
      self::_checkDirExist($app_path . '/controllers');
      self::_checkDirExist($app_path . '/models');
      self::_checkDirExist($app_path . '/views');
      self::_checkDirExist($app_path . '/libraries');
      self::_checkDirExist($app_path . '/third_party');
      self::_checkDirExist($app_path . '/modules');

      if(!is_array($config))
         throw new SystemException($config_file . 
                 ' should return array'); 
      
      self::_checkKeyInConfig('param');
      
      if(!is_array($config['param']))
         throw new SystemException('"param" should be array ' . 
                 $config_file);  
      
      self::_checkKeyInConfig('db');
      
      if(!isset($config['db']['dsn']))
         throw new SystemException('Key "dsn" not exist in db. Please ' . 
                 'refer for dsn in PDO. file: ' . $config_file);
      
      if(!isset($config['db']['username']))
         throw new SystemException('Key "username" not exist in db. ' . 
                 'file: ' . $config_file);  
      
      if(!isset($config['db']['password']))
         throw new SystemException('Key "password" not exist in db. ' . 
                 'file: ' . $config_file); 
      
      self::_checkKeyInConfig('default_controller');
      
      self::_checkKeyInConfig('error_page');
      
      self::_checkKeyInConfig('domain');
      
      if(!is_array($config['error_page']))
         throw new SystemException('"error_page" should be array ' . 
                 $config_file);  
      
      if(!isset($config['error_page']['controller']))
         throw new SystemException('error_page should have "controller" key' . 
                 ' in ' . $config_file); 
      
      if(!isset($config['error_page']['error404']))
         throw new SystemException('error_page should have "error404" key' . 
                 ' in ' . $config_file);   
      
      if(!isset($config['error_page']['error500']))
         throw new SystemException('error_page should have "error500" key' . 
                 ' in ' . $config_file);   
      
      if($config['default_controller'] == '' || 
              empty($config['default_controller']) || 
              $config['default_controller'] == null)
         throw new SystemException('"default_controller" cannot be empty in ' . 
                 $config_file);    
      
      $controller_file = ucfirst($config['default_controller']) . 
         'Controller.php';
      
      $controller_file_path = $app_path . '/controllers/' . $controller_file;
      if(!file_exists($controller_file_path))
         throw new SystemException($controller_file_path . ' file not exist');
      
      
   }
}
